package com.sxt.opd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.bus.domain.Goods;
import com.sxt.bus.domain.Provider;
import com.sxt.opd.domain.Patient;
import com.sxt.opd.service.PatientService;
import com.sxt.opd.vo.PatientVo;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/patient")
public class PatientController {


        @Autowired
        private PatientService patientService;

        /**
         * 前台传日期，后台Date接值类型转换错误
         * @param binder
         * @param request
         */
        @InitBinder
        public void InitBinder(WebDataBinder binder, WebRequest request)
        {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            CustomDateEditor editor = new CustomDateEditor(df,true);//参数为true表示允许为空值
            binder.registerCustomEditor(Date.class, editor);
        }

        /**
         * 查询
         */
        @RequestMapping("loadAllPatient")
        public DataGridView loadAllPatient(PatientVo patientVo) {
            IPage<Patient> page = new Page<>(patientVo.getPage(), patientVo.getLimit());
            QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(patientVo.getId() != null,"id",patientVo.getId());
            queryWrapper.like(StringUtils.isNotBlank(patientVo.getPatientname()), "patientname",
                    patientVo.getPatientname());
            queryWrapper.like(StringUtils.isNotBlank(patientVo.getPhone()), "phone", patientVo.getPhone());
            this.patientService.page(page, queryWrapper);
            return new DataGridView(page.getTotal(), page.getRecords());
        }

        /**
         * 添加
         */
        @RequestMapping("addPatient")
        public ResultObj addPatient(PatientVo patientVo) {
            try {
                this.patientService.save(patientVo);
                return ResultObj.ADD_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.ADD_ERROR;
            }
        }

        /**
         * 修改
         */
        @RequestMapping("updatePatient")
        public ResultObj updatePatient(PatientVo patientVo) {
            try {
                this.patientService.updateById(patientVo);
                return ResultObj.UPDATE_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.UPDATE_ERROR;
            }
        }

        /**
         * 删除
         */
        @RequestMapping("deletePatient")
        public ResultObj deletePatient(Integer id) {
            try {
                this.patientService.removeById(id);
                return ResultObj.DELETE_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.DELETE_ERROR;
            }
        }

        /**
         * 批量删除
         */
        @RequestMapping("batchDeletePatient")
        public ResultObj batchDeletePatient(PatientVo patientVo) {
            try {
                Collection<Serializable> idList = new ArrayList<Serializable>();
                for (Integer id : patientVo.getIds()) {
                    idList.add(id);
                }
                this.patientService.removeByIds(idList);
                return ResultObj.DELETE_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.DELETE_ERROR;
            }
        }
    /**
     *根据供应商ID查询商品信息
     */
    @RequestMapping("loadPatientById")
    public DataGridView loadPatientById(Integer id) {
        Patient patient = null;
        try {
            patient = this.patientService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new DataGridView(patient);
    }





}

